package com.ly.cloud.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.ly.cloud.dto.RoleUserQueryDto;
import com.ly.cloud.dto.UserDeleteDto;
import com.ly.cloud.exception.CloudException;
import com.ly.cloud.exception.mapper.DBException;
import com.ly.cloud.mapper.StudentInfoMapper;
import com.ly.cloud.mapper.TeacherMapper;
import com.ly.cloud.mapper.UserManagerMapper;
import com.ly.cloud.service.UserManagerService;
import com.ly.cloud.vo.UserRoleVo;

/**
 * 用户管理服务接口
 * @author luhuacong
 * @date   2019/1/2
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
public class UserManagerServiceImpl implements UserManagerService {

	@Autowired
	private UserManagerMapper userManagerMapper;
	
	@Autowired
	private TeacherMapper teacherMapper;	//老师Mapper
	
	@Autowired
	private StudentInfoMapper studentInfoMapper;	//学生Mapper

	/** 
	 * selectUserListByCon
	 * Description: 查询用户信息
	 * @date: 2018年1月2日
	 */
	@Override
	public Page<UserRoleVo> selectUserListByCon(Page<UserRoleVo> page, RoleUserQueryDto dto) {
		
		try {
			return page.setRecords(userManagerMapper.selectRoleUserList(page, dto));
		} catch (Exception e) {
			e.printStackTrace();
			throw new CloudException("查询用户信息失败",e);
		}
	}
	
	/** 
	 * Method Name: deleteUserById
	 * Description:  
	 * 根据用户类型和账号删除用户
	 * @param dto 删除用户封装的条件
	 * @date: 2018年1月2日
	 */
	@Override
	public boolean deleteUserById(UserDeleteDto dto) {
		Integer i = 0;
		if("1".equals(dto.getYhlx())) {
			//如果用户类型是老师，则从老师表中删除
			try {
				i = teacherMapper.deleteById(dto.getZh());
			} catch (Exception e) {
				e.printStackTrace();
				throw new DBException("删除用户失败",e);
			}
		}
		if("0".equals(dto.getYhlx())) {
			//如果用户类型是学生，则从学生表中删除
			try {
				i = studentInfoMapper.deleteById(dto.getZh());
			} catch (Exception e) {
				e.printStackTrace();
				throw new DBException("删除用户失败",e);
			}
		}
		if(i>0) {
			return true;
		}
		return false;
	}

	/** 
	 * Method Name: deleteUserList
	 * Description:  
	 * 批量删除用户信息
	 * @param dtoList 删除用户封装的条件对象集合
	 * @date: 2018年1月2日
	 */
	@Override
	@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
	public Integer deleteUserList(List<UserDeleteDto> dtoList) {
		Integer count = 0;	//成功删除的个数
		Integer i = 0;
		for (UserDeleteDto userDeleteDto : dtoList) {
			if("1".equals(userDeleteDto.getYhlx())) {
				//如果用户类型是老师，则从老师表中删除
				try {
					i = teacherMapper.deleteById(userDeleteDto.getZh());
					count = count + i;
				} catch (Exception e) {
					e.printStackTrace();
					throw new DBException("批量删除用户失败",e);
				}
			}
			if("0".equals(userDeleteDto.getYhlx())) {
				//如果用户类型是学生，则从学生表中删除
				try {
					i = studentInfoMapper.deleteById(userDeleteDto.getZh());
					count = count + i;
				} catch (Exception e) {
					e.printStackTrace();
					throw new DBException("批量删除用户失败",e);
				}
			}
		}
		return count;
	}
}